package com.lxd.sunso.product.controller;


import com.alibaba.fastjson.JSON;
import com.lxd.sunso.common.utils.R;
import com.lxd.sunso.product.components.ConversionUtil;
import com.lxd.sunso.product.netty.domain.ApnVo;
import com.lxd.sunso.product.netty.domain.Device;
import com.lxd.sunso.product.netty.domain.ServerInfo;
import com.lxd.sunso.product.netty.domain.SocketVo;
import com.lxd.sunso.product.netty.util.CacheUtil;
import com.lxd.sunso.product.service.NettyService;
import com.lxd.sunso.product.vo.MachineData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


@Controller
@RequestMapping("/netty")
public class NettyController {

    @Autowired
    private NettyService nettyService;

    private Logger logger = LoggerFactory.getLogger(NettyController.class);

    @RequestMapping("/queryNettyServerList")
    @ResponseBody
    public Collection<ServerInfo> queryNettyServerList() {
        try {
            Collection<ServerInfo> serverInfos = CacheUtil.serverInfoMap.values();
            return serverInfos;
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping("/queryDeviceList")
    @ResponseBody
    public Collection<Device> queryDeviceList() {
        try {
            Collection<Device> deviceInfos = CacheUtil.deviceGroup.values();
            logger.info("查询设备链接列表。{} JSON.toJSONString(deviceInfos)", JSON.toJSONString(deviceInfos));
            return deviceInfos;
        } catch (Exception e) {
            logger.info("查询设备链接列表失败。", e);
            return null;
        }
    }

    /**
     * 参数设置
     *
     * @param machineData
     */
    @RequestMapping("/doSendMsg")
    @ResponseBody
    public void doSendMsg(@RequestBody MachineData machineData) {
        nettyService.sendMsgToMachine(machineData);
    }

    //测试
    @RequestMapping("/doMsg")
    @ResponseBody
    public MachineData doMsg() {
        MachineData machineData = new MachineData();
        List<String> machineNameList = new ArrayList<>();
        machineNameList.add("test0001");
        machineNameList.add("test0002");
        machineNameList.add("test0003");
        machineNameList.add("test0004");
        String adress = "01";
        String value = "100";
        machineData.setMachineName(machineNameList);
        machineData.setAddress(adress);
        machineData.setValue(value);
        return machineData;
    }

    /**
     * 召测
     *
     * @param machineName
     */
    @RequestMapping("/call/{machineName}")
    @ResponseBody
    public void call(@PathVariable String machineName) {
        nettyService.call(machineName);
    }

    /**
     * 重启
     */
    @RequestMapping("/restart/{machineName}")
    @ResponseBody
    public void restart(@PathVariable String machineName) {
        nettyService.restart(machineName);
    }

    /**
     * 返回设备的socket值
     */
    @RequestMapping("/socket/{machineName}")
    @ResponseBody
    public R getSocket(@PathVariable String machineName) {
        SocketVo socket = nettyService.getSocket(machineName);
        return R.ok().put("data", socket);
    }

    /**
     * 下发数据设置socket
     */
    @RequestMapping("/setSocket/{machineName}")
    @ResponseBody
    public R setSocket(@PathVariable String machineName, @RequestBody SocketVo socketVo) {
        nettyService.setSocket(machineName, socketVo);
        return R.ok();
    }

    /**
     * 查询并返回APN的参数值
     */
    @RequestMapping("/apn/{machineName}")
    @ResponseBody
    public R getApn(@PathVariable String machineName) {
        ApnVo apnVo = nettyService.getApn(machineName);
        return R.ok().put("data", apnVo);
    }


    /**
     * 下发数据设置apn
     */
    @RequestMapping("/setApn/{machineName}")
    @ResponseBody
    public R setApn(@PathVariable String machineName, @RequestBody ApnVo apnVo) {
        nettyService.setApn(machineName, apnVo);
        return R.ok();
    }


    /**
     * 服务器下发版本查询消息给设备
     */
    @RequestMapping("/getVersion/{machineName}")
    @ResponseBody
    public R getVersion(@PathVariable String machineName) {
        String version = nettyService.getVersion(machineName);
        return R.ok().put("data", version);
    }

    //接收前端上传的文件
    @RequestMapping("/saveFile")
    @ResponseBody
    public R saveFile(@RequestParam("file") MultipartFile newFile) throws Exception {
        nettyService.saveFile(newFile);
        return R.ok();
    }

    //清除map中的bin文件
    @RequestMapping("/deleteBin")
    @ResponseBody
    public R deleteBin() throws Exception {
        nettyService.deleteBin();
        return R.ok();
    }
}
